package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 护理分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<NursingProject>> page = nursingProjectMapper.selectByPage(name, status);
        return PageResponse.of(page, NursingProjectVo.class);
    }


    /**
     * 根据id查询护理项目
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo selectById(Long id) {
        //主要逻辑
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        if (nursingProject == null) {
            throw new BaseException(BasicEnum.NURSING_NOT_EXIST_ERROR);
        }
        NursingProjectVo nursingProjectVo = new NursingProjectVo();
        BeanUtil.copyProperties(nursingProject, nursingProjectVo);

        return nursingProjectVo;
    }


    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> getAllProject() {
        List<NursingProject> nursingProject = nursingProjectMapper.getAllProject();
        List<NursingProjectVo> nursingProjectVos = nursingProject.stream()
                .map(project -> {
                    NursingProjectVo vo = new NursingProjectVo();
                    BeanUtil.copyProperties(project, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        return nursingProjectVos;
    }

    /**
     * 新增
     * @param nursingProjectDto
     */
    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = nursingProjectMapper.selectByName(nursingProjectDto.getName());
        if (nursingProject != null) {
            throw new BaseException(BasicEnum.NURSING_INSERT_DUPLICATION_ERROR);
        }
        //主要逻辑
        NursingProject addNursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDto, addNursingProject);
        nursingProjectMapper.addNursingProject(addNursingProject);
    }


    /**
     * 编辑
     * @param nursingProjectDto
     */
    @Override
    public void edit(NursingProjectDto nursingProjectDto) {
        Long relativeId = nursingProjectPlanMapper.selectRelativeId(nursingProjectDto.getId());

        Long planId = nursingProjectPlanMapper.selectPlanId(relativeId);
        NursingPlan nursingPlan = nursingPlanMapper.selectById(planId);
        if (nursingPlan != null) {
            throw new BaseException(BasicEnum.NURSING_PLAN_EXIST_ERROR);
        }else {  NursingProject nursingProject = new NursingProject();
            BeanUtil.copyProperties(nursingProjectDto, nursingProject);
            //主要逻辑
            nursingProjectMapper.edit(nursingProject);
        }

    }

    /**
     * 启用禁用
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        Long relativeId = nursingProjectPlanMapper.selectRelativeId(id);
        Long planId = nursingProjectPlanMapper.selectPlanId(relativeId);
        NursingPlan nursingPlan = nursingPlanMapper.selectById(planId);
        if (nursingPlan != null) {
            throw new BaseException(BasicEnum.NURSING_PLAN_EXIST_ERROR);
        }
        //主要逻辑
        nursingProjectMapper.enableOrDisable(id, status);
    }


    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteProject(Long id) {
        Long relativeId = nursingProjectPlanMapper.selectRelativeId(id);
        Long planId = nursingProjectPlanMapper.selectPlanId(relativeId);
        NursingPlan nursingPlan = nursingPlanMapper.selectById(planId);
        if (nursingPlan != null) {
            throw new BaseException(BasicEnum.NURSING_PLAN_EXIST_ERROR);
        }
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        if (Objects.equals(nursingProject.getStatus(),1)){
            throw new BaseException(BasicEnum.NURSING_STATUS_ERROR);
        }
        //主要逻辑
        nursingProjectMapper.deleteProject(id);
    }
}